package com.sth.shishan;

import org.junit.Test;
import redis.clients.jedis.Jedis;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author: sitianhong
 * @Description: 网站每日UV数据指标去重统计
 * set 数据结构,更java里面的set是一样的,就是说无序的,不重复的数据集合,list一般来说
 * @Since: create in 2021/6/13 0013 19:59
 */
public class RedisDemo8 {
    Jedis jedis = new Jedis("127.0.0.1", 6378);

    @Test
    public void demo() {
        for (int i = 0; i < 100; i++) {
            long userId = i + 1;
            for (int j = 0; j < 10; j++) {
                addUserAccess(userId);
            }
        }
        long uv = getUV();
        System.out.println("当日UV " + uv);
    }

    // 添加一次用户的访问记录
    public void addUserAccess(long userId) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String today = dateFormat.format(new Date());
        jedis.sadd("user_acess_set::" + today, String.valueOf(userId));

    }

    public long getUV() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String today = dateFormat.format(new Date());
        return jedis.scard("user_acess_set::" + today);
    }
}
